package bTree.LeetCode;

import java.util.HashSet;

public class 有效的数独 {
    public boolean isValidSudoku(char[][] board) {
        int row = board.length;
        int line = board[0].length;
        for (int i = 0; i < row; i++) {
            HashSet<Character> hashSet = new HashSet<>();
            for (int j = 0; j < line; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet.contains(c)) {
                        hashSet.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        for (int j = 0; j < line; j++) {
            HashSet<Character> hashSet = new HashSet<>();
            for (int i = 0; i < row; i++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet.contains(c)) {
                        hashSet.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet1 = new HashSet<>();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet1.contains(c)) {
                        hashSet1.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet2 = new HashSet<>();
        for (int i = 0; i < 3; i++) {
            for (int j = 3; j < 6; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet2.contains(c)) {
                        hashSet2.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet3 = new HashSet<>();
        for (int i = 0; i < 3; i++) {
            for (int j = 6; j < 9; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet3.contains(c)) {
                        hashSet3.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet4 = new HashSet<>();
        for (int i = 3; i < 6; i++) {
            for (int j = 0; j < 3; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet4.contains(c)) {
                        hashSet4.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet5 = new HashSet<>();
        for (int i = 3; i < 6; i++) {
            for (int j = 3; j < 6; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet5.contains(c)) {
                        hashSet5.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet6 = new HashSet<>();
        for (int i = 3; i < 6; i++) {
            for (int j = 6; j < 9; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet6.contains(c)) {
                        hashSet6.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet7 = new HashSet<>();
        for (int i = 6; i < 9; i++) {
            for (int j = 0; j < 3; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet6.contains(c)) {
                        hashSet6.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet8 = new HashSet<>();
        for (int i = 6; i < 9; i++) {
            for (int j = 3; j < 6; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet6.contains(c)) {
                        hashSet6.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        HashSet<Character> hashSet9 = new HashSet<>();
        for (int i = 6; i < 9; i++) {
            for (int j = 6; j < 9; j++) {
                char c = board[i][j];
                if (c >= '0' && c <= '9') {
                    if (!hashSet6.contains(c)) {
                        hashSet6.add(c);
                    } else {
                        return false;
                    }
                }
            }
        }
        return true;

    }
}